package cn.aitrox.ry.app.web.controller.config;

import cn.aitrox.ry.app.web.vo.ResultOutVo;
import cn.aitrox.ry.app.web.vo.config.common.in.CommonListInVo;
import cn.aitrox.ry.app.web.vo.config.common.out.MaxCodeOutVo;
import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.config.dto.CommonConfigDto;
import cn.aitrox.ry.service.config.dto.CommonConfigItemDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigChildAddInDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigChildEditInDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigNameExistInDto;
import cn.aitrox.ry.service.config.dto.in.CommonConfigSearchInDto;
import cn.aitrox.ry.service.config.feign.CommonConfigFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CommonConfigController {

    @Autowired
    @Lazy
    private CommonConfigFeign commonConfigFeign;

    @PostMapping(value = "/config/common/list")
    public RespResult<CommonConfigDto> commonList(@RequestBody CommonListInVo input) {
        CommonConfigDto commonConfig = commonConfigFeign.commonList(input.getConfigList());
        return RespResult.ok(commonConfig);
    }

    @GetMapping(value = "/config/common/maxCode")
    public RespResult<MaxCodeOutVo> maxCode(@RequestParam("codePrefix") String codePrefix) {
        String maxCode = commonConfigFeign.maxCode(codePrefix);
        return RespResult.ok(maxCode);
    }

    @PostMapping(value = "/config/common/child")
    public RespResult childAdd(@Validated @RequestBody CommonConfigChildAddInDto input) {
        return commonConfigFeign.childAdd(input);
    }

    @PutMapping(value = "/config/common/child")
    public RespResult childEdit(@Validated @RequestBody CommonConfigChildEditInDto input) {
        return commonConfigFeign.childEdit(input);
    }

    @PostMapping(value = "/config/common/search")
    public RespResult<CommonPageInfo<CommonConfigItemDto>> search(@Validated @RequestBody CommonConfigSearchInDto input) {
        CommonPageInfo<CommonConfigItemDto> pageInfo = commonConfigFeign.search(input);
        return RespResult.ok(pageInfo);
    }

    @PostMapping(value = "/config/common/name/existed")
    public RespResult<ResultOutVo> nameExisted(@Validated @RequestBody CommonConfigNameExistInDto input) {
        Boolean existed = commonConfigFeign.nameExisted(input);
        return RespResult.ok(ResultOutVo.build(existed));
    }
}
